﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessModel;
using System.Web.Script.Serialization;
using PessMvc.Models;

namespace PessMvc.Controllers
{
    [Authorize]
    [NotNeedToChangePassword]
    public class PAuthorController : Controller
    {
        private PESSEntities db = new PESSEntities();

        //
        // GET: /PAuthor/

        public ActionResult Index(int id)
        {
            var pauthors = db.PAuthors.Where(pa=>pa.ArchiveID==id).OrderBy(pa=>pa.Order);//.Include(p => p.Archive);
            ViewBag.Archive = db.Archives.Where(a => a.ID == id).First();
            //ViewBag.ArchiveID = id;
            return View(pauthors.ToList());
        }

        public ActionResult ChangeAuthors(int id)
        {
            var pauthors = db.PAuthors.Where(pa => pa.ArchiveID == id).OrderBy(pa => pa.Order);//.Include(p => p.Archive);
            ViewBag.Archive = db.Archives.Where(a => a.ID == id).First();
            //ViewBag.ArchiveID = id;
            return View(pauthors.ToList());
        }


        //
        // GET: /PAuthor/Details/5

        public ActionResult Details(int id = 0)
        {
            PAuthor pauthor = db.PAuthors.Find(id);
            if (pauthor == null)
            {
                return HttpNotFound();
            }
            return View(pauthor);
        }

        //
        // GET: /PAuthor/Create

        public ActionResult Create(int archiveid)
        {
            ViewBag.Archive = db.Archives.Where(a=>a.ID==archiveid);//new SelectList(db.Archives, "ID", "ProjectTitle");

            return View();
        }

        //
        // POST: /PAuthor/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(PAuthor pauthor, string action, string controller)
        {
            if (ModelState.IsValid)
            {
                db.PAuthors.Add(pauthor);
                db.SaveChanges();
                return RedirectToAction(action, controller, new {id=pauthor.ArchiveID});
            }

            ViewBag.ArchiveID = new SelectList(db.Archives, "ID", "ProjectTitle", pauthor.ArchiveID);
            return View(pauthor);
        }

       

        //
        // GET: /PAuthor/Edit/5

        public ActionResult Edit(int id = 0)
        {
            PAuthor pauthor = db.PAuthors.Find(id);
            if (pauthor == null)
            {
                return HttpNotFound();
            }
            ViewBag.ArchiveID = new SelectList(db.Archives, "ID", "CountyCode", pauthor.ArchiveID);
            return View(pauthor);
        }

        //
        // POST: /PAuthor/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(PAuthor pauthor)
        {
            if (ModelState.IsValid)
            {
                db.Entry(pauthor).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ArchiveID = new SelectList(db.Archives, "ID", "CountyCode", pauthor.ArchiveID);
            return View(pauthor);
        }

        //
        // GET: /PAuthor/Delete/5

        public ActionResult Delete(string a, string c, int id = 0 )
        {
            PAuthor pauthor = db.PAuthors.Find(id);
            int archiveid = pauthor.ArchiveID;
            db.PAuthors.Remove(pauthor);
            db.SaveChanges();
            return RedirectToAction(a, c, new { id = archiveid });
        }



        //
        // POST: /PAuthor/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            PAuthor pauthor = db.PAuthors.Find(id);
            db.PAuthors.Remove(pauthor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Move(int id, int nid, string a, string c)
        {
            PAuthor a1= db.PAuthors.Find(id);
            int old_order = a1.Order;
            PAuthor a2 = db.PAuthors.Find(nid);
            a1.Order = a2.Order;
            a2.Order = old_order;
            db.Entry(a1).State = EntityState.Modified;
            db.Entry(a2).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction(a, c, new { id = a1.ArchiveID });
            
        }

        [HttpPost]
        public JsonResult CreateAjax(int archiveid, string email, string lastname, string firstname, string country, string organization, int order, int authortype)
        {
            PAuthor pauthor = new PAuthor() { ArchiveID = archiveid, EMail = email, LastName = lastname, FirstName = firstname, Country = country, Organization = organization, Order = order,Type=authortype };
            db.PAuthors.Add(pauthor);
            db.SaveChanges();
            var pauthors = from pa in db.PAuthors
                           where pa.ArchiveID == archiveid
                           orderby pa.Type descending, pa.Order
                           select new
                           {
                               Email = pa.EMail,
                               Name = pa.LastName + "," + pa.FirstName,
                               Organization = pa.Organization,
                               Country = pa.Country,
                               Order = pa.Order,
                               ID = pa.ID,
                               Type = (AuthorType)pa.Type
                           };
            return Json(pauthors.ToList(), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult DeleteAjax(int id)
        {
            PAuthor pauthor = db.PAuthors.Find(id);
            int archiveid = pauthor.ArchiveID;
            db.PAuthors.Remove(pauthor);
            db.SaveChanges();
            var pauthors = from pa in db.PAuthors
                           where pa.ArchiveID == archiveid
                           orderby pa.Type descending, pa.Order
                           select new
                           {
                               Email = pa.EMail,
                               Name = pa.LastName + "," + pa.FirstName,
                               Organization = pa.Organization,
                               Country = pa.Country,
                               Order = pa.Order,
                               ID = pa.ID,
                               Type = (AuthorType)pa.Type
                           };
            return Json(pauthors.ToList(), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult MoveAjax(int id, int nid)
        {
            PAuthor a1 = db.PAuthors.Find(id);
            int archiveid = a1.ArchiveID;
            int old_order = a1.Order;
            PAuthor a2 = db.PAuthors.Find(nid);
            a1.Order = a2.Order;
            a2.Order = old_order;
            db.Entry(a1).State = EntityState.Modified;
            db.Entry(a2).State = EntityState.Modified;
            db.SaveChanges();
            var pauthors = from pa in db.PAuthors
                           where pa.ArchiveID == archiveid
                           orderby pa.Type descending, pa.Order
                           select new
                           {
                               Email = pa.EMail,
                               Name = pa.LastName + "," + pa.FirstName,
                               Organization = pa.Organization,
                               Country = pa.Country,
                               Order = pa.Order,
                               ID = pa.ID,
                               Type=(AuthorType)pa.Type
                           };
            return Json(pauthors.ToList(), JsonRequestBehavior.AllowGet);

        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}